/**
* 系统管理 管理员
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>管理员管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" class="user-search">
      <el-form-item label="">
        <el-input size="small" v-model="username" placeholder="输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-input size="small" v-model="mobile" placeholder="输入手机号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="getdata(1)">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" @selection-change="selectChange" :data="userData" highlight-current-row v-loading="loading"
      border element-loading-text="拼命加载中" style="width: 100%">
      <el-table-column align="center" type="index" label="序号" width="60">
      </el-table-column>

      <el-table-column align="center" prop="username" label="用户名" width="120">
      </el-table-column>
      <el-table-column align="center" prop="name" label="姓名" width="120">
      </el-table-column>
      <el-table-column align="center" prop="mobile" label="手机号" width="120">
      </el-table-column>
      <el-table-column align="center" prop="sex" label="性别" min-width="50">
      </el-table-column>
      <el-table-column align="center" prop="rolesId" label="角色权限" min-width="100">
        <template slot-scope="scope">
          <el-button size="mini">{{ typeName(scope.row.rolesId) }}</el-button>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="300">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>

    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog('edit')">
      <el-form label-width="80px" ref="editForm" :model="editForm">
        <el-form-item label="用户名" prop="username">
          <el-input size="small" v-model="editForm.username" auto-complete="off" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input size="small" v-model="editForm.name" auto-complete="off" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="rolesId">
          <el-select size="small" v-model="editForm.rolesId" placeholder="请选择" class="userRole">
            <el-option v-for="item in rolesList" :key="item.value" :disabled="item.disabled" :label="item.rolesName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input size="small" v-model="editForm.mobile" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="mobile">
          <el-input size="small" v-model="editForm.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio v-model="editForm.sex" label="男">男</el-radio>
          <el-radio v-model="editForm.sex" label="女">女</el-radio>
        </el-form-item>

        <el-form-item label="城市名称" prop="sex" v-if="editForm.rolesId==2">
          <el-input size="small" v-model="editForm.addressName" placeholder="请输入城市昵称"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog('edit')">取消</el-button>

        <el-button size="small" type="primary" :loading="loading" class="title" @click="addUser()"
          v-if="title == '添加用户'">添加</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="upUser()"
          v-if="title == '修改用户'">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 导入请求方法
import {
  addAdmin, getAdminList, deleteAdmin,upAdmin
} from "../../api/userMG";
import Pagination from "../../components/Pagination";
export default {
  data() {
    return {
      username: "", //搜索名字
      mobile: "", //搜索手机号
      rolesList: [
        {
          rolesName: '超级管理员',
          id: 1,
          disabled: true,
        },
        {
          rolesName: '地方管理员',
          id: 2,
          disabled: false,
        },
      ], //角色列表
      // 编辑与添加
      editForm: {
        username: "",
        password: "",
        name: "",
        mobile: "",
        sex: "",
        rolesId: "",
        addressName: '',
      },

      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      title: "添加用户",
      editFormVisible: false, //控制编辑页面显示与隐藏
      dataAccessshow: false, //控制数据权限显示与隐藏
      unitAccessshow: false, //控制所属单位隐藏与显示

      // 选择数据
      selectdata: [],
      // rules表单验证

      //用户数据
      userData: [],

      // 选中
      checkmenu: [],
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 20,
        total: 10,
      },
      stationList: [],
      stationArr: ""
    };
  },
  // 注册组件
  components: {
    Pagination,
  },

  /**
   * 数据发生改变
   */
  watch: {},

  /**
   * 创建完毕
   */
  created() {
    this.getdata(1);
  },
  computed: {
    typeName() {
      return id => {
        let str = ''
        this.rolesList.forEach(item => {
          if (item.id == id) {
            str = item.rolesName
            return
          }
        })
        return str
      }
    }
  },
  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    getProjectArr(projectArr) {
      if (!projectArr) {

        return "";
      }
      var str = "";
      var arr = JSON.parse(projectArr)
      this.projectList.forEach(element => {

        arr.forEach(id => {

          if (element.id == id) {
            str = str + element.projectName + ","
          }

        });

      });
      return str;
    },


    // 获取数据方法
    getdata(page) {
      this.editFormVisible = false;
      this.loading = true;
      // page = (page - 1) * 20;
      // 获取用户列表
      var parameter = {
        username: this.username,
        mobile: this.mobile,
        page: (page - 1) * 20,
      };
      getAdminList(parameter).then((res) => {
        this.loading = false;
        if (res.code != 200) {
          this.$message({
            type: "info",
            message: res.msg,
          });
        } else {
          this.userData = res.data.list;
          // 分页赋值
          this.pageparm.currentPage = page;
          this.pageparm.total = res.data.count;
        }
      });
    },


    // 分页插件事件
    callFather(parm) {
      // this.formInline.page = parm.currentPage;
      // this.formInline.limit = parm.pageSize;
      this.getdata(parm.currentPage);
    },


    //显示编辑界面
    handleEdit: function (index, row) {
      this.editFormVisible = true;
      if (row != undefined && row != "undefined") {
        this.title = "修改用户";
        this.editForm.id = row.id
        this.editForm.username = row.username
        this.editForm.password = row.password
        this.editForm.name = row.name
        this.editForm.mobile = row.mobile
        this.editForm.sex = row.sex
        this.editForm.rolesId = row.rolesId
        this.editForm.addressName = row.addressName
      } else {
        this.title = "添加用户";
        this.editForm.username = "";
        this.editForm.password = "";
        this.editForm.name = "";
        this.editForm.mobile = "";
        this.editForm.sex = "";
        this.editForm.rolesId = "";
        this.editForm.addressName = "";
      }
    },
    // 添加用户
    addUser() {

      this.loading = true;
      var editForm = this.editForm;

      addAdmin(editForm).then((res) => {
        this.loading = false;
        if (res.code == 200) {
          this.$message({
            type: "success",
            message: res.msg,
          });
          this.getdata(1);
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    // 修改用户
    upUser() {
      this.loading = true;
      var editForm = this.editForm;

      upAdmin(editForm).then((res) => {
        this.loading = false;
        if (res.code == 200) {
          this.$message({
            type: "success",
            message: res.msg,
          });
          this.getdata(1);
        } else {
          this.$message.error(res.msg);
        }
      });
    },


    // 选择复选框事件
    selectChange(val) {
      this.selectdata = val;
    },
    // 关闭编辑、增加弹出框
    closeDialog(dialog) {
      if (dialog == "edit") {
        this.editFormVisible = false;
      } else if (dialog == "perm") {
        this.dataAccessshow = false;
      } else if (dialog == "unit") {
        this.unitAccessshow = false;
      }
    },
    // 删除用户
    deleteUser(index, row) {
      this.$confirm("确定要删除吗?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 删除
          deleteAdmin({ id: row.id })
            .then((res) => {
              if (res.code == 200) {
                this.$message({
                  type: "success",
                  message: "数据已删除!",
                });
                this.getdata(1);
              } else {
                this.$message({
                  type: "info",
                  message: res.msg,
                });
              }
            })
            .catch((err) => {
              this.loading = false;
              this.$message.error("数据删除失败，请稍后再试！");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除！",
          });
        });
    },


    // 刷新缓存
    refreshCache(index, row) {
      userFlashCache(row.userName)
        .then((res) => {
          if (res.success) {
            this.$message({
              type: "success",
              message: "刷新成功！",
            });
            this.getdata(this.formInline);
          } else {
            this.$message({
              type: "info",
              message: res.msg,
            });
          }
        })
        .catch((err) => {
          this.loading = false;
          this.$message.error("刷新失败，请稍后再试！");
        });
    },
  },
};
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}

.userRole {
  width: 100%;
}
</style>